<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2020/7/13
 * Time: 15:28
 */

namespace app\components\Api;

use app\components\API;
use app\components\Lcms;
use JMessage\IM\Message;
use JMessage\JMessage;
use Yii;
use JMessage\IM\User;

class JMessageApi
{
    const appKey = 'ebc58098e670c47189b05396';
    const masterSecret = 'f5f03abfc9cf44b535cd5293';

    const qianzhui = '';


    /**
     * @ apiGroup 用户
     * @ api {post} /api/app/call/getjiguang 获取极光配置信息
     * @ apiSuccess {string} data[0] appKey
     * @ apiSuccess {string} data[1] 随机字符串
     * @ apiSuccess {string} data[2] timestamp
     * @ apiSuccess {string} data[3] signature
     * @ apiSuccess {string} data[4] jiguang_id
     * @ apiSuccess {string} data[0] jiguang_pass
     * @ apiSuccessExample 返回
     *
     *{"code":0,
     * "data":["ebc58098e670c47189b05396","5f905183efb61ae3712450eae60f3fd1","1594772731123","2a16fcb99367ad690f33938319280c1f","mc_3fcf7c5918d8abc602cff4b5f528b8b","54bdffcb3c378d2c1144e21206e613e"]}
     * @ apiUse ERROR
     */
    public static function getjiguang()
    {
        $random_str = md5(time());
        $timestamp = time() . '123';
        $signature = md5('appkey=' . self::appKey
            . '&timestamp=' . $timestamp . '&random_str=' . $random_str . '&key=' . self::masterSecret);

        //  $user = OA::$user->getCacheModel();
        $user = Yii::$app->user->getCacheModel();
        $data = [
            self::appKey,
            $random_str,
            $timestamp,
            $signature,
            $user['jiguang_id'],
            $user['jiguang_pass']
        ];
        return API::echo_json(0, $data);//exit;
    }

    /**
     * 返回极光配置信息
     */
    public static function getJiguangArray()
    {
        $random_str = md5(time());
        $timestamp = time() . '123';
        $signature = md5('appkey=' . self::appKey
            . '&timestamp=' . $timestamp . '&random_str=' . $random_str . '&key=' . self::masterSecret);

        //  $user = OA::$user->getCacheModel();
        $user = Yii::$app->user->getCacheModel();
        $data = [
            self::appKey,
            $random_str,
            $timestamp,
            $signature,
            $user['jiguang_id'],
            $user['jiguang_pass']
        ];
        return $data;
    }

    public static function getclient()
    {
        return new JMessage(self::appKey, self::masterSecret, ['disable_ssl' => true]);
    }

    /**
     * 修改用户昵称
     */
    public static function upNameNick($uid, $name)
    {
        $userModel = \app\models\User::find()->where(['id' => $uid])->select('jiguang_id')->limit(1)->one();
        if (!$userModel) {
            return false;
        }
        $uid = $userModel->jiguang_id;
        $client = self::getclient();
        $user = new \JMessage\IM\User($client);
        return $user->update($uid, [
            'nickname' => $name
        ]);
    }

    /**
     * 修改密码
     */
    public static function updatePassword($uid, $password)
    {
        $client = self::getclient();
        $user = new User($client);
        return $user->updatePassword($uid, $password);
    }


    /**
     * 注册极光用户
     */
    public static function register($uid, $password)
    {
        $client = self::getclient();
        $user = new \JMessage\IM\User($client);
        return $user->register($uid, $password);
    }


    /**
     * 产生一个极光id
     */
    public static function getRandChar()
    {
        return 'mc_' . md5(rand(100, 99999999) . time());
    }


    /**
     * 使用系统账号向用户发送消息
     */
    public static function SendMsg($uid, $msg, $extras = [])
    {
        $jiguangid = self::idToJiguang($uid);
        //   var_dump($jiguangid);exit;
        $client = self::getclient();
        $message = new Message($client);
        /// $uid=self::qianzhui.$uid;
        $r = $message->sendText(
            '1',                ///$version: 版本号
            [
                'type' => 'admin',
                'id' => 'admin',///mc_1
                // 'name'=>'美策客服'
            ],                          ///$from: 发送者信息数组
            [
                'type' => 'single',
                'id' => $jiguangid
            ],                              ///$target: 接受者信息数组
            [
                'text' => $msg,
                'extras' => $extras
            ],                              //$msg: 消息体数组
            [
                'ttss' => 'aaaa'
            ],                          ///$notification: 自定义通知数组
            [
                'offline' => 'true',
                ///   'vv'=>'vvvvv'
            ]                           ///$options: 其他选项数组


        );
        /**
         * $version: 版本号
         * $from: 发送者信息数组
         * $target: 接受者信息数组
         * $msg: 消息体数组
         * $notification: 自定义通知数组
         * $options: 其他选项数组
         */
        return $r;
    }


    /**
     * 发送带有标题和描述的消息
    */
    public static function SendMsgTitleDesc($uid, $title,$desc,$pic=''){

        if($pic==''){
            $pic=Lcms::$LOGO;
        }
        $msg=$pic.$title.$desc;
        $jiguangid = self::idToJiguang($uid);
        //   var_dump($jiguangid);exit;
        $client = self::getclient();
        $message = new Message($client);
        /// $uid=self::qianzhui.$uid;
        $r = $message->sendCustom(
            '1',                ///$version: 版本号
            [
                'type' => 'admin',
                'id' => 'admin',///mc_1
                // 'name'=>'美策客服'
            ],                          ///$from: 发送者信息数组
            [
                'type' => 'single',
                'id' => $jiguangid
            ],                              ///$target: 接受者信息数组
            [
                'title' => $title,
                'desc' => $desc,
                'pic' => $pic,
                'extras' => []
            ],                              //$msg: 消息体数组
            [
                'ttss' => 'aaaa'
            ],                          ///$notification: 自定义通知数组
            [
                'offline' => 'true',
                ///   'vv'=>'vvvvv'
            ]                           ///$options: 其他选项数组


        );
        /**
         * $version: 版本号
         * $from: 发送者信息数组
         * $target: 接受者信息数组
         * $msg: 消息体数组
         * $notification: 自定义通知数组
         * $options: 其他选项数组
         */
        return $r;
    }




    /**
     * 用户id转换为极光id
     */
    public static function idToJiguang($id)
    {
        $one = \app\models\User::find()->where([
            'id' => $id
        ])->select([
            'jiguang_id'
        ])->one();
        if ($one) {
            return $one->jiguang_id;
        } else {
            return null;
        }
    }


    public function getuserListhead(Request $request)
    {
        $namelist = $request->post('namelist');
        $list = explode(",", $namelist);
        $model = UserModel::wherein('jiguang_id', $list)->select([
            'jiguang_id', 'head'
        ])->get();
        $listAll = [];
        foreach ($model as $vo) {
            $listAll[$vo->jiguang_id] = $vo->getHead();
        }
        return $listAll;
    }

}